package com.bl.weilan.manager.controller.user;


import com.bl.weilan.common.ApiResponseBody;
import com.bl.weilan.manager.controller.BaseController;
import com.bl.weilan.service.biz.user.UserService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.user.UserInfoManageModifyVo;
import com.bl.weilan.service.vo.user.UserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by yujingyi on 2018/1/6.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/weilan/manager/user"})
@Api(value = "用户相关api", description = "用户相关api")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "用户列表", notes = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "name", value = "昵称", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "status", value = "状态", required = false, paramType = "query", dataType = "String", defaultValue = ""),
            @ApiImplicitParam(name = "page", value = "页数", required = true, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/get_list_multi"})
    public ApiResponseBody<UserListVo> getListMulti(
            @RequestParam(value = "id", required = false, defaultValue = "") String id,
            @RequestParam(value = "mobile", required = false, defaultValue = "") String mobile,
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam(value = "status", required = false, defaultValue = "") String status,
            @RequestParam(value = "page", required = true, defaultValue = "1") int page,
            @RequestParam(value = "size", required = true, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        UserListVo userListVo = userService.getUserListMulti(id, mobile, name, status, pageable);

        return ApiResponseBody.createSuccessBody(userListVo);
    }


    @ApiOperation(value = "删除批量", notes = "删除批量用户信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del_list"})
    public ApiResponseBody delList(
            @Valid @RequestBody DelListBody body, BindingResult result,
            HttpServletRequest request) {

        userService.delList(body);

        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "删除单个用户信息", notes = "删除单个用户信息 ")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/del"})
    public ApiResponseBody del(
            @Valid @RequestBody DelBody body, BindingResult result,
            HttpServletRequest request) {


        userService.del(body);

        return ApiResponseBody.createSuccessBody(null);
    }


    @ApiOperation(value = "修改用户信息", notes = "修改用户信息 ")
    @ApiImplicitParams({})
    @PostMapping(value = {"/modify"})
    public ApiResponseBody managerModify(
            @Valid @RequestBody UserInfoManageModifyVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        userService.modify(body);

        return ApiResponseBody.createSuccessBody(null);
    }
}
